from wtforms.widgets import TextArea
from wtforms.fields import TextAreaField

from flask_admin.contrib.sqla import ModelView as BaseModelView

class CKTextAreaWidget(TextArea):
    def __call__(self, field, **kwargs):
        if kwargs.get('class'):
            kwargs['class'] = str(kwargs['class']) + ' ckeditor'
        else:
            kwargs.setdefault('class', 'ckeditor')
        return super(CKTextAreaWidget, self).__call__(field, **kwargs)
 
class CKTextAreaField(TextAreaField):
    widget = CKTextAreaWidget()
 


class MyHouseView(BaseModelView):
    column_labels = {
        'below300': '三层以下300m²',
        'above300': '三层以下超出300m²',
        'above0to100': '三层以上0-100m²',
        'above100to200': '三层以上100-200m²',
        'above200to300': '三层以上200-300m²',
        'brick': '砖结构金额',
        'simple': '简结构金额',
        'shed': '棚结构金额',
        'other': '其他结构金额',
        'decoration': '装修补偿费',
        'move': '房屋搬迁补偿费',
        'facility': '附属设施补偿',
        'temporary': '临时安置补偿费',
        'signing': '按期签约腾退奖励费',
        'business1': '经营腾退补助费',
        'business2': '困难补助费',
        'total': '总补偿金额'
    }
    
    column_list = ['total','below300', 'above300', 'above0to100','above100to200','above200to300','brick','simple','shed','other','decoration','move','facility','temporary','signing','business1','business2']  # 只显示这些字段
    column_searchable_list = ['below300', 'above300']  # 这些字段可搜索



class ArticleAdmin(BaseModelView):
    extra_js = ['//cdn.ckeditor.com/4.22.1/standard/ckeditor.js']
    # extra_js = ['/static/ckeditor/ckeditor.js']
    form_overrides = {
        'content': CKTextAreaField
    }

    form_widget_args = {
        'content': {
            'rows': 10,
            'style': 'width: 100%'
        }
    }
    # 中文标签
    column_labels = {
        'title': '政策标题',
        'content': '政策内容',
        'publish_date': '发布时间'
    }
    
    # 表单中文标签
    form_labels = column_labels
    
    # 设置列表页显示的字段
    column_list = ['title', 'publish_date']
    
    # 设置可搜索字段
    column_searchable_list = ['title']
    
    # 设置可排序字段
    column_sortable_list = ['publish_date']
    